package com.sanchunhui.psc.utils.StatusBarUtil

import android.os.Build
import android.os.Environment
import android.text.TextUtils

import java.io.File
import java.io.FileInputStream
import java.util.Properties


class RomUtils {
    internal object AvailableRomType {
        val MIUI = 1
        val FLYME = 2
        val ANDROID_NATIVE = 3
        val NA = 4
    }

    companion object {

        //开发版 7.7.13 及以后版本采用了系统API，旧方法无效但不会报错
        val lightStatusBarAvailableRomType: Int
            get() {
                if (isFlymeV4OrAbove) {
                    return AvailableRomType.FLYME
                }
                if (isMiUIV7OrAbove) {
                    return AvailableRomType.ANDROID_NATIVE
                }

                if (isMiUIV6OrAbove) {
                    return AvailableRomType.MIUI
                }

                return if (isAndroidMOrAbove) {
                    AvailableRomType.ANDROID_NATIVE
                } else AvailableRomType.NA

            }

        //Flyme V4的displayId格式为 [Flyme OS 4.x.x.xA]
        //Flyme V5的displayId格式为 [Flyme 5.x.x.x beta]
        private//版本号4以上，形如4.x.
        val isFlymeV4OrAbove: Boolean
            get() {
                val displayId = Build.DISPLAY
                if (!TextUtils.isEmpty(displayId) && displayId.contains("Flyme")) {
                    val displayIdArray =
                        displayId.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                    for (temp in displayIdArray) {
                        if (temp.matches("^[4-9]\\.(\\information_listen+\\.)+\\S*".toRegex())) {
                            return true
                        }
                    }
                }
                return false
            }

        //Android Api 23以上
        private val isAndroidMOrAbove: Boolean
            get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M

        private val KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code"

        private val isMiUIV6OrAbove: Boolean
            get() {
                try {
                    val properties = Properties()
                    properties.load(
                        FileInputStream(
                            File(
                                Environment.getRootDirectory(),
                                "build.prop"
                            )
                        )
                    )
                    val uiCode = properties.getProperty(KEY_MIUI_VERSION_CODE, null)
                    if (uiCode != null) {
                        val code = Integer.parseInt(uiCode)
                        return code >= 4
                    } else {
                        return false
                    }

                } catch (e: Exception) {
                    return false
                }

            }

        internal val isMiUIV7OrAbove: Boolean
            get() {
                try {
                    val properties = Properties()
                    properties.load(
                        FileInputStream(
                            File(
                                Environment.getRootDirectory(),
                                "build.prop"
                            )
                        )
                    )
                    val uiCode = properties.getProperty(KEY_MIUI_VERSION_CODE, null)
                    if (uiCode != null) {
                        val code = Integer.parseInt(uiCode)
                        return code >= 5
                    } else {
                        return false
                    }

                } catch (e: Exception) {
                    return false
                }

            }
    }

}
